package com.weicx.wuzhiops.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weicx.wuzhiops.constant.MessageConstant;
import com.weicx.wuzhiops.constant.event.EventKeys;
import com.weicx.wuzhiops.entity.EntryProperty;
import com.weicx.wuzhiops.entity.ExportProperty;
import com.weicx.wuzhiops.exception.BusinessException;
import com.weicx.wuzhiops.mapper.ExportPropertyMapper;
import com.weicx.wuzhiops.model.request.ExportPropertyRequest;
import com.weicx.wuzhiops.model.wrapper.DataGrid;
import com.weicx.wuzhiops.service.ExportPropertyService;
import com.weicx.wuzhiops.util.BeanMapUtil;
import com.weicx.wuzhiops.util.EventParamsHolder;
import com.weicx.wuzhiops.util.Valid;
import com.weicx.wuzhiops.vo.EntryPropertyVO;
import com.weicx.wuzhiops.vo.ExportPropertyVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出库资产表 服务实现类
 * </p>
 *
 * @author caoshi
 * @since 2022-12-12
 */
@Service
public class ExportPropertyServiceImpl implements ExportPropertyService {

    @Resource
    private ExportPropertyMapper exportPropertyMapper;

    @Override
    public Long addExportProperty(ExportPropertyRequest request){
        ExportProperty insert = new ExportProperty();
        insert.setName(request.getName());
        insert.setClassroom(request.getClassroom());
        insert.setCount(request.getCount() == null ? 1 : request.getCount());
        insert.setRepairFactory(request.getRepairFactory());
        insert.setRepairEndTime(request.getRepairEndTime());
        insert.setEntryTime(LocalDateTime.now());
        insert.setExportTime(LocalDateTime.now());
        insert.setExportUser(request.getExportUser());
        insert.setCreateTime(LocalDateTime.now());


        exportPropertyMapper.insert(insert);

    // 设置日志参数
        EventParamsHolder.addParams(BeanMapUtil.bean2Map(insert));
        return insert.getId();
    }

    @Override
    public Integer deleteExportProperty(List<Long> idList) {
        int effect = 0;
        for (Long id : idList) {
            effect += exportPropertyMapper.deleteById(id);
        }

        // 设置日志参数
        EventParamsHolder.addParam(EventKeys.ID_LIST, idList);
        EventParamsHolder.addParam(EventKeys.COUNT, effect);
        return effect;
    }

    @Override
    public Integer updateExportProperty(ExportPropertyRequest request){

        Long id = request.getId();
        ExportProperty exportProperty = exportPropertyMapper.selectById(id);
        Valid.notNull(exportProperty, MessageConstant.UNKNOWN_DATA);

        ExportProperty update = new ExportProperty();
        update.setId(id);
        update.setName(request.getName());
        update.setClassroom(request.getClassroom());
        update.setCount(request.getCount() == null ? 1 : request.getCount());
        update.setRepairFactory(request.getRepairFactory());
        update.setRepairEndTime(request.getRepairEndTime());
        update.setEntryTime(LocalDateTime.now());
        update.setExportTime(LocalDateTime.now());
        update.setExportUser(request.getExportUser());
        update.setCreateTime(LocalDateTime.now());

        return exportPropertyMapper.updateById(update);
    }

    @Override
    public DataGrid<ExportPropertyVO> listExportProperty(ExportPropertyRequest request){
        Page<ExportProperty> page = new Page<>(request.getPage(), request.getLimit());

        LambdaQueryWrapper<ExportProperty> wrapper = new LambdaQueryWrapper<ExportProperty>()
                .like(StringUtils.isNotBlank(request.getName()), ExportProperty::getName, request.getName())
                .like(StringUtils.isNotBlank(request.getClassroom()), ExportProperty::getClassroom, request.getClassroom())
                .like(StringUtils.isNotBlank(request.getExportUser()), ExportProperty::getExportUser, request.getExportUser())
                .orderByDesc(ExportProperty::getCreateTime);

        Page<ExportProperty> exportPropertyPage = exportPropertyMapper.selectPage(page, wrapper);
        Page<ExportPropertyVO> voPage = new Page<>();
        voPage.setSize(exportPropertyPage.getSize());
        voPage.setCurrent(exportPropertyPage.getCurrent());
        voPage.setPages(exportPropertyPage.getPages());
        voPage.setTotal(exportPropertyPage.getTotal());
        List<ExportProperty> records = exportPropertyPage.getRecords();
        List<ExportPropertyVO> voRecords = new ArrayList<>();
        try {
            for (ExportProperty exportProperty : records) {
                ExportPropertyVO vo = new ExportPropertyVO();
                BeanUtils.copyProperties(vo, exportProperty);
                voRecords.add(vo);
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new BusinessException(MessageConstant.OPERATOR_ERROR);
        }
        voPage.setRecords(voRecords);
        return DataGrid.of(voPage);
    }

    @Override
    public ExportProperty getExportProperty(Long id){

        ExportProperty exportProperty = exportPropertyMapper.selectById(id);
        if (exportProperty == null) {
            throw new BusinessException(MessageConstant.UNKNOWN_DATA);
        }

        ExportProperty vo = new ExportProperty();
        try {
            BeanUtils.copyProperties(vo, exportProperty);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new BusinessException(MessageConstant.OPERATOR_ERROR);
        }

        return vo;

    }

}
